package com.mall.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.mall.commons.utils.PageResult;
import com.mall.domain.PmsProduct;
import com.mall.domain.SmsFlashPromotionProductRelation;
import com.mall.interface1.api.SmsFlashPromotionProductRelationApi;
import com.mall.vo.FlashRelationVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SmsFlashPromotionProductRelationService {

    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    //批量选择商品添加关联
    public void create(List<SmsFlashPromotionProductRelation> relationList) {
        if(CollUtil.isNotEmpty(relationList)){
            for (SmsFlashPromotionProductRelation item : relationList) {
                smsFlashPromotionProductRelationApi.create(item);
            }
        }
    }

    //删除关联
    public void delete(Long id) {
        smsFlashPromotionProductRelationApi.deleteById(id);
    }

    //分页查询不同场次关联及商品信息
    public PageResult list(Map map, Integer pageNum, Integer pageSize) {
        List<FlashRelationVo> voList = new ArrayList<>();
        Long flashPromotionId = (Long) map.get("flashPromotionId");
        Long flashPromotionSessionId = (Long) map.get("flashPromotionSessionId");
        //根据id分页查询出关联表信息
        List<SmsFlashPromotionProductRelation> sfpprList = smsFlashPromotionProductRelationApi.list(flashPromotionId,flashPromotionSessionId,pageNum,pageSize);
        for (SmsFlashPromotionProductRelation item : sfpprList) {
            FlashRelationVo vo = new FlashRelationVo();
            BeanUtil.copyProperties(item,vo);
            //根据产品id查询产品
            PmsProduct pmsProduct = smsFlashPromotionProductRelationApi.findById(item.getProductId());
            vo.setProduct(pmsProduct);
            voList.add(vo);
        }
        //获取关联商品总数
        Integer count = smsFlashPromotionProductRelationApi.count(flashPromotionId,flashPromotionSessionId);
        return new PageResult(pageNum,pageSize,count,voList);
    }

    //修改关联信息
    public void update(Long id, SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelation.setId(id);
        smsFlashPromotionProductRelationApi.update(smsFlashPromotionProductRelation);
    }

    //获取管理商品促销信息
    public SmsFlashPromotionProductRelation find(Long id) {
        return smsFlashPromotionProductRelationApi.find(id);
    }
}
